<template>
	<view class="container">
		<mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ native: true }"
			@down="downCallback" :up="upOption" @up="upCallback">
			<!-- tab栏 -->
			<u-tabs style="background-color: #fff;" :list="tabs" :current="curTab" :activeStyle="{color:'#FF0000'}"
				@click="onChangeTab"></u-tabs>

			<!-- 订单列表 -->
			<view class="order-list">
				<view class="order-item" v-for="(item, index) in list.data" :key="index">
					<view class="item-top">
						<view class="item-top-left">
							<u-icon name="file-text-fill"></u-icon>
							<text class="order-time">订单编号 {{ item.id }}</text>
						</view>
						<view class="item-top-right">
							<text class="state-text">{{ item.pageStatus }}</text>
						</view>
					</view>
					<!-- 商品列表 -->
					<view class="goods-list">
						<view class="goods-item" v-for="(goods, idx) in item.orderItemVos" :key="idx">
							<!-- 商品图片 -->
							<view class="goods-image">
								<image class="image" :src="goods.pic ? (goods.pic.startsWith('https') ? goods.pic : 'https://saas.hzjhckj.com/api' + goods.pic) : 'https://building-d.oss-cn-hangzhou.aliyuncs.com/img/img.jpg'" mode="scaleToFill"></image>
							</view>
							<!-- 商品信息 -->
							<view class="goods-content">
								<view class="goods-title"><text class="twoline-hide">{{ goods.name }}</text>
								</view>
								<view class="goods-props clearfix">
									<view class="goods-props-item"
										v-for="(v,i)  in JSON.parse(goods.specification)" :key="i">
										<text>{{ v.key }}：</text>
										<text>{{ v.value }}</text>
									</view>
								</view>
							</view>
							<!-- 交易信息 -->
							<view class="goods-trade">
								<view class="goods-price">
									<text class="unit">￥</text>
									<text class="value">{{ goods.price }}</text>
								</view>
								<view class="goods-num">
									<text>×{{ goods.productQuantity }}</text>
								</view>
							</view>
						</view>
					</view>
					<!-- 订单合计 -->
					<view class="order-total" v-if="item.pageStatus == '待付款'">
						<text>应付款</text>
						<text class="unit">￥</text>
						<text class="money">{{ item.payAmount }}</text>
					</view>
					<view class="order-total" v-if="item.pageStatus == '已付款'">
						<text>实付款</text>
						<text class="unit">￥</text>
						<text class="money">{{ item.realPayAmount }}</text>
					</view>
					<!-- 订单操作 -->
					<view class="order-handle">
						<view class="btn-group clearfix">
							<!-- 未支付取消订单 -->
							<block v-if="item.pageStatus == '待付款'">
								<view class="btn-item" @click="onCancel(item.id)">取消订单</view>
							</block>
							<!-- 未支付的订单 -->
							<block v-if="item.pageStatus == '待付款'">
								<view class="btn-item active" @click="onPay(item.order_id)">去付款</view>
							</block>
							<!-- 确认收货 -->
							<block v-if="item.pageStatus == '待收货'">
								<view class="btn-item active" @click="onReceipt(item.id)">确认收货</view>
							</block>
							<!-- 确认收货 -->
							<block v-if="item.pageStatus == '待付款'">
								<view class="btn-item" @click="onDelete(item.id)">删除订单</view>
							</block>

						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	// import {
	//   DeliveryStatusEnum,
	//   DeliveryTypeEnum,
	//   OrderStatusEnum,
	//   PayStatusEnum,
	//   ReceiptStatusEnum
	// } from '@/common/enum/order'
	import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins'
	import {
		getEmptyPaginateObj,
		getMoreListData
	} from '@/core/app'
	import {
		listMallOrderProducts,
		confireOrder,
		cancelOrder,
		deleteOrder
	} from '@/api/order'

	// 每页记录数量
	const pageSize = 15

	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				// // 枚举类
				// DeliveryStatusEnum,
				// DeliveryTypeEnum,
				// OrderStatusEnum,
				// PayStatusEnum,
				// ReceiptStatusEnum,

				// 当前页面参数
				options: {
					dataType: 'all'
				},
				// tab栏数据
				tabs: [{
					name: `待付款`,
					value: 1
				}, {
					name: `待发货`,
					value: 2
				}, {
					name: `待收货`,
					value: 3
				}, {
					name: `已完成`,
					value: 7
				}],
				// 当前标签索引
				curTab: 0,
				// 订单列表数据
				list: {
					data: []
				},

				// 上拉加载配置
				upOption: {
					// 首次自动执行
					auto: true,
					// 每页数据的数量; 默认10
					page: {
						size: pageSize
					},
					// 数量要大于4条才显示无更多数据
					noMoreSize: 4,
					// 空布局
					empty: {
						tip: '亲，暂无订单记录'
					}
				},
				// 控制onShow事件是否刷新订单列表
				canReset: false,
				orderStatus: 1
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			this.curTab = options.current
			this.orderStatus = options.orderStatus
		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {
			this.canReset && this.onRefreshList()
			this.canReset = false
		},

		/**
		 * 生命周期函数--监听页面的卸载
		 */
		onUnload() {
			// 卸载全局事件订阅
			uni.$off('syncRefresh')
		},

		methods: {

			/**
			 * 上拉加载的回调 (页面初始化时也会执行一次)
			 * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
			 * @param {Object} page
			 */
			async upCallback(page) {
				const res = await listMallOrderProducts({
					current: page.num,
					size: 10,
					orderStatus: this.orderStatus
				})
				this.list.data = res
				const curPageLen = res.length
				const totalSize = res.total
				this.mescroll.endBySize(curPageLen, totalSize)

			},
			// 初始化订单列表数据
			initList(newList) {
				newList.data.forEach(item => {
					item.total_num = 0
					item.goods.forEach(goods => {
						item.total_num += goods.total_num
					})
				})
				return newList
			}, 
			onChangeTab(val) {
				this.orderStatus = val.value
				// 刷新优惠券列表
				// app.onRefreshList()
				this.list.data = []
				this.mescroll.resetUpScroll()
			},

			// 刷新订单列表
			onRefreshList() {
				this.list.data = []
				setTimeout(() => {
					this.mescroll.resetUpScroll()
				}, 120)
			},

			// 取消订单
			onCancel(orderId) {
				const app = this
				uni.showModal({
					title: '友情提示',
					content: '确认要取消该订单吗？',
					success: async (o) => {
						if (o.confirm) {
							const res = await cancelOrder({
								orderId: orderId
							})
							uni.showToast({
								title:'取消成功'
							})
							this.onRefreshList()
						}
					}
				});
			},

			// 确认收货
			onReceipt(orderId) {
				const app = this
				uni.showModal({
					title: '友情提示',
					content: '确认收到商品了吗？',
					success: async (o) => {
						if (o.confirm) {
							const res = await confireOrder({
								orderId: orderId
							})
							uni.showToast({
								title:'成功'
							})
							this.onRefreshList()
						}
					}
				});
			},
			onDelete(orderId) {
				const app = this
				uni.showModal({
					title: '删除',
					content: '确认删除订单吗？',
					success: async (o) => {
						if (o.confirm) {
							const res = await deleteOrder({
								orderId: orderId
							})
							uni.showToast({
								title:'成功'
							})
							this.onRefreshList()
						}
					}
				});
			},
			// 点击去支付
			onPay(orderId) {
				this.$navTo('pages/checkout/cashier/index', {
					orderId
				})
			},

			// 跳转到订单详情页
			handleTargetDetail(orderId) {
				this.$navTo('pages/order/detail', {
					orderId
				})
			},

			// 跳转到订单评价页
			handleTargetComment(orderId) {
				this.$navTo('pages/order/comment/index', {
					orderId
				})
			}

		},

	}
</script>

<style>
	page {
		background: #f7f8fa;
	}
</style>
<style lang="scss" scoped>
	//文字平均分配

	::v-deep .u-tabs__wrapper__nav[data-v-48634e29] {
		display: flex;
		flex-direction: row;
		position: relative;
		justify-content: space-between;
	}

	//选中蓝色滑块对应

	::v-deep .u-tabs__wrapper__nav__item {
		flex: 1 1 0% !important;
	}

	.order-list{
		padding: 0 20rpx;
		box-sizing: border-box;
	}
	// 项目内容
	.order-item {
		margin: 20rpx auto 0 auto;
		padding: 30rpx 30rpx;
		width: 100%;
		box-sizing: border-box;
		box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
		border-radius: 16rpx;
		background: #fff;
	}

	// 项目顶部
	.item-top {
		display: flex;
		justify-content: space-between;
		font-size: 26rpx;
		margin-bottom: 40rpx;

		.item-top-left {
			display: flex;
			align-items: center;

			.order-time {
				color: #777;
				margin-left: 10rpx;
			}
		}


		.state-text {
			color: #FF0000;
		}
	}

	// 商品列表
	.goods-list {
overflow: hidden;
		// 商品项
		.goods-item {
			display: flex;
			margin-bottom: 40rpx;

			// 商品图片
			.goods-image {
				width: 180rpx;
				height: 180rpx;

				.image {
					display: block;
					width: 100%;
					height: 100%;
					border-radius: 8rpx;
				}
			}

			// 商品内容
			.goods-content {
				flex: 1;
				padding-left: 16rpx;
				padding-top: 16rpx;

				.goods-title {
					font-size: 28rpx;
					max-height: 76rpx;
					font-weight: 700;
				}

				.goods-props {
					margin-top: 14rpx;
					color: #ababab;
					font-size: 24rpx;
					overflow: hidden;

					.goods-props-item {
						padding: 4rpx 16rpx;
						border-radius: 12rpx;
						background-color: #fcfcfc;
					}
				}


			}

			// 交易信息
			.goods-trade {
				padding-top: 16rpx;
				width: 150rpx;
				text-align: right;
				// color: $uni-text-color-grey;
				font-size: 26rpx;

				.goods-price {
					vertical-align: bottom;
					margin-bottom: 16rpx;

					.unit {
						margin-right: -2rpx;
						font-size: 24rpx;
					}
				}
			}

		}

	}

	// 订单合计
	.order-total {
		font-size: 26rpx;
		vertical-align: bottom;
		text-align: right;
		height: 40rpx;
		margin-bottom: 30rpx;
		font-weight: 700;

		.unit {
			margin-left: 8rpx;
			margin-right: -2rpx;
			font-size: 32rpx;
		}

		.money {
			font-size: 34rpx;
		}
	}

	// 订单操作
	.order-handle {
		.btn-group {

			.btn-item {
				border-radius: 10rpx;
				padding: 8rpx 20rpx;
				margin-left: 15rpx;
				font-size: 26rpx;
				float: right;
				color: #383838;
				border: 1rpx solid #a8a8a8;

				&:last-child {
					margin-left: 0;
				}

				&.active {
					color: #fff;
					background-color: #FF0000;
					border: 1rpx solid #FF0000;
				}
			}

		}

	}
</style>